package com.szit.deviceserver.server.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szit.deviceserver.dao.DeviceUserMapper;
import com.szit.deviceserver.entity.DeviceUser;
import com.szit.deviceserver.server.DeviceUserServer;
import com.szit.util.CodeMsg;
import com.szit.util.MD5Util;
import com.szit.util.Result;
import com.szit.util.vo.AdminUserVo;
import com.szit.util.vo.DeviceUserInfo;
import com.szit.util.vo.DeviceUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @author wyler
 * @date 2020-10-16
 */
@Slf4j
@Service
public class deviceUserServerImpl implements DeviceUserServer {

    @Autowired
    private DeviceUserMapper deviceUserMapper;

    /**
     * 后台添加设备用户
     *
     * @param deviceUserInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertDeviceUser(DeviceUserInfo deviceUserInfo) {
        try {
            if(StringUtils.isEmpty(deviceUserInfo) &&
                    StringUtils.isEmpty(deviceUserInfo.getUsername()) &&
                    StringUtils.isEmpty(deviceUserInfo.getPassword())){
                log.info("deviceUserInfo--------参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }

            log.info("--------添加设备用户--------");
            String nslat = UUID.randomUUID().toString().replaceAll("-","");
            String nPass = MD5Util.getMD5(deviceUserInfo.getPassword(),nslat);

            DeviceUser deviceUser = new DeviceUser();
            deviceUser.setUsername(deviceUserInfo.getUsername());
            deviceUser.setPassword(nPass);
            deviceUser.setSalt(nslat);
            deviceUser.setState(0);
            deviceUser.setCreatetime(new Date());
            deviceUserMapper.insertSelective(deviceUser);


            return Result.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            /*手工回滚异常*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }

    /**
     * 后台删除设备用户
     *
     * @param deviceUserInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteDeviceUser(DeviceUserInfo deviceUserInfo) {
        try {
            if(StringUtils.isEmpty(deviceUserInfo) &&
                    StringUtils.isEmpty(deviceUserInfo.getId())){
                log.info("deviceUserInfo--------参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }

            log.info("--------查询设备用户是否存在--------");
            DeviceUser deviceUser = deviceUserMapper.selectByPrimaryKey(deviceUserInfo.getId());
            if(StringUtils.isEmpty(deviceUser)){
                log.info("--------设备用户不存在--------");
                return Result.error(CodeMsg.NOT_FIND_DATA);
            }

            log.info("--------删除设备用户--------");
            deviceUserMapper.deleteByPrimaryKey(deviceUserInfo.getId());

            return Result.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            /*手工回滚异常*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }

    /**
     * 后台更新设备用户
     *
     * @param deviceUserInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateDeviceUser(DeviceUserInfo deviceUserInfo) {
        try {
            if(StringUtils.isEmpty(deviceUserInfo) &&
                    StringUtils.isEmpty(deviceUserInfo.getId())){
                log.info("deviceUserInfo--------参数为空");
                return Result.error(CodeMsg.PARAMETER_ISNULL);
            }

            log.info("--------查询设备用户是否存在--------");
            DeviceUser deviceUser = deviceUserMapper.selectByPrimaryKey(deviceUserInfo.getId());
            if(StringUtils.isEmpty(deviceUser)){
                log.info("--------设备用户不存在--------");
                return Result.error(CodeMsg.NOT_FIND_DATA);
            }

            log.info("--------更新设备用户--------");
            deviceUser.setUsername(deviceUserInfo.getUsername());
            if(!StringUtils.isEmpty(deviceUserInfo.getPassword())){
                String nslat = UUID.randomUUID().toString().replaceAll("-","");
                String nPass = MD5Util.getMD5(deviceUserInfo.getPassword(),nslat);
                deviceUser.setPassword(nPass);
                deviceUser.setSalt(nslat);
            }
            deviceUser.setAuthorizationcode(deviceUserInfo.getAuthorizationcode());
            deviceUser.setValidity(deviceUserInfo.getValidity());
            deviceUser.setState(deviceUserInfo.getState());
            deviceUserMapper.updateByPrimaryKeySelective(deviceUser);


            return Result.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            /*手工回滚异常*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }

    /**
     * 查询所有设备用户
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result selectAllDeviceUser(Integer pageNum, Integer pageSize) {
        try {
            if(StringUtils.isEmpty(pageNum)){
                pageNum = 1;
            }
            if(StringUtils.isEmpty(pageSize)){
                pageSize = 10;
            }

            log.info("--------查询所有用户---------");
            /*分页插件 传入页数和每页数量*/
            PageHelper.startPage(pageNum, pageSize);
            ArrayList<DeviceUserVo> list = deviceUserMapper.selectAllDeviceUser();
            PageInfo pageInfo = new PageInfo(list);

            return Result.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_EXCEPTION);
        }
    }
}
